package com.itheima.controller;

import com.itheima.domain.PageBean;
import com.itheima.domain.User;
import com.itheima.service.Impl.UserServiceImpl;
import com.itheima.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/user/*")
public class UserServlet extends BaseServlet {
        /*
        查询所有

         */
      //需要多次用到service  放到全局
    private UserService userService = new UserServiceImpl();

    private void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //字符集统一
        req.setCharacterEncoding("UTF-8");

        resp.setContentType("text/html;charset=utf-8");
        //当前页码的设置
        Integer pageNum = 1;//默认访问第一页
        //获取前段传递的当前页码
        String pageNum1 = req.getParameter("pageNum");
        if (pageNum1!=null){
            pageNum=Integer.parseInt(pageNum1);
        }

        //获取总记录数1
        Long count = userService.findCount();
        List<User> list = userService.finAll(pageNum);
        //计算总页数;总记录数和每页显示条目计算
        long pages = (count % 3 == 0) ? (count / 3) : (count / 3 + 1);

        PageBean pb = new PageBean();
        pb.setPageNum(pageNum);
        pb.setTotal(count);
        pb.setPages(pages);
        pb.setList(list);

        //调用service

        req.setAttribute("pb",pb);
//        req.setAttribute("pageNum",pageNum);
//        req.setAttribute("total",count);
//        req.setAttribute("pages",pages);

        req.getRequestDispatcher("/list.jsp").forward(req,resp);

    }
}
